//
//  RegiestViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/11.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "RegiestViewController.h"

@interface RegiestViewController ()<UINavigationControllerDelegate,UIPickerViewDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (weak, nonatomic) IBOutlet UITextField *number;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property(nonatomic,strong)NSString *ipone;
@property(nonatomic,strong)NSString *pass;
@property(nonatomic,strong)
NSString *getdata;
@property (weak, nonatomic) IBOutlet UITextField *getNUmber;
@property(nonatomic,strong)NSString *myName;
@property(nonatomic,strong)UIImage *image;
@end

@implementation RegiestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"注册";
    
    self.head.layer.cornerRadius = 50;
    self.head.layer.masksToBounds = YES;
    
    
}
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -------------回收键盘--------
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (UITextField *textField in self.view.subviews)
    {
        [textField resignFirstResponder];
    }
}
//注册
- (IBAction)regiest:(UIButton *)sender {
    _getdata = _getNUmber.text;
    _myName = _name.text;
    _ipone = _number.text;
    _pass = _password.text;
    //转化成加密模式
    _pass = [NSString md5:_pass];
    [RequestManger requestWithURL:[NSString stringWithFormat:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=register&password=%@&account=%@&checkcode=%@&nickname=%@&invitationCode=",_pass,_ipone,_getdata,_myName] requestType:@"RequestGet" parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic);
        NSString *string = dic[@"description"];
        NSString *result = dic[@"result"];
        if ([result isEqualToString:@"ok"]) {
            string = @"注册成功";
        }
        UIAlertController *control = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle: UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [control addAction:action];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self presentViewController:control animated:YES completion:nil];
        });

    } error:nil];
    
}
//获取验证码
- (IBAction)getIphoneNumber:(UIButton *)sender {
    _ipone = _number.text;
    _pass = _password.text;
    [RequestManger requestWithURL:[NSString stringWithFormat:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=checkcode&account=%@",_ipone] requestType:@"RequestGet" parDic:nil finish:^(NSData *data) {
       // NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    } error:nil];

}
- (IBAction)pickHeadImage:(UITapGestureRecognizer *)sender
{
    UIAlertController *control = [UIAlertController alertControllerWithTitle:@"选择照片" message:@"请选择照片" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了相册");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            picker.delegate = self;
            //是否可以编辑
            picker.allowsEditing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    [control addAction:action];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了相机");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    [control addAction:action2];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [control addAction:action3];
    [self presentViewController:control animated:YES completion:nil];
    
}
#pragma mark ----------执行拍摄完成后或旋转照片后自动执行的方法----------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //得到图片
    self.image = info[UIImagePickerControllerOriginalImage];
    //存储到本地
    NSData *data = UIImagePNGRepresentation(_image);
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"image"];
    //保存图片
    UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
    [self dismissViewControllerAnimated:YES completion:nil];
    self.head.image = _image;
}


    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
